home *** CD-ROM | disk | FTP | other *** search
/ The Arsenal Files 8 / The Arsenal Files Collection #8 (Arsenal Computer) (1996).ISO / prg_casm / snpd9611.zip / ENVIRON.TXT < prev    next >
Text File  |  1996-11-24  |  3KB  |  58 lines

  1. .I 0 56
  2. +++Date last modified: 29-Oct-1996
  3.  
  4. Q. Is it possible to set an environment variable from within a program? I've
  5.    tried setnev() and it doesn't work.
  6.  
  7.  
  8. A. Generally, the answer is no. First of all, you must understand how
  9.    environment variables work. The command shell (COMMAND.COM, 4DOS, or any
  10.    of the Unix shells) is what maintains the environment variables. They are
  11.    stored, usually concatenated, in a block of memory owned by the command
  12.    shell. When your program runs, it inherits a copy of the environment
  13.    block. This is a crucial concept! The command shell only copies its
  14.    "master environment" and passes the copy to the program. When the program
  15.    terminates, its copy, which was in memory it owned, is discarded along
  16.    with any changes which were made to it. This is why putenv() seems not to
  17.    work, since it only modifies the copy and the changes are lost upon
  18.    program termination. Ultimately, the question you must answer is which
  19.    copy of the environment you wish to modify? If you only wish to modify
  20.    your local environment, use putenv(). If you want to modify something
  21.    else, you have more questions to answer. Remember that several copies of
  22.    the command shell may be running at a time, so which one's environment
  23.    block do you want to modify? If you choose to modify your parent process'
  24.    environment block, that too will be lost when your parent process
  25.    terminates. If you want to modify the original master, then your changes
  26.    may still be lost when you terminate since your parent process may not be
  27.    the "master" command shell (see below for an illustration).
  28.  
  29.    It's a can of worms that the makers of most operating systems (OS's),
  30.    including DOS, Unix, OS/2, Win95, et al, have wisely chosen to leave
  31.    sealed!
  32.  
  33.  
  34.  
  35. Q. What good is putenv() if its changes are lost when the program ends?
  36.  
  37.  
  38. A. Its primary use is to set up environment variables prior to spawning
  39.    another subordinate process (program). In the PC world, you'd use it prior
  40.    to issuing a spawn call. In the Unix world, use it prior to using fork()
  41.    and exec().
  42.  
  43.  
  44.  
  45. Q. I know I've seen DOS programs which do modify the master environment, so I
  46.    know it can be done!
  47.  
  48.  
  49. A. Yes it can, but not legally or with guaranteed results. Most OS's do have
  50.    undocumented ways of doing it. DOS is probably the safest to try since
  51.    it's not multiuser or multitasking, and so the only person you can screw
  52.    up is yourself! There are 4 ways to modify a non-local environment under
  53.    DOS. Only one is marginally legal and the others rely on undocumented DOS
  54.    features.
  55.  
  56.  
  57.  
  58.